/**************************************************************************** 
* 
* Copyright (c) 2022  C*Core -   All Rights Reserved  
* 
* THIS SOFTWARE IS DISTRIBUTED "AS IS," AND ALL WARRANTIES ARE DISCLAIMED, 
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* 
* PROJECT     : CCFC201XBC           
* DESCRIPTION : CCFC201XBC MPU low level drivers Code  
* HISTORY     : 2022
* @file     mpu_lld.c
* @version  3.0
* @date     2022-10-27
* @brief    Initial version.
*
*****************************************************************************/
#include "mpu_lld.h"
#include "e200z3_asm.h"

/*************************************************************************
 * The MPU supports a maximum of 8 slaves, while the CCFC2011BC supports 
 *   only 3 slaves.
 * A maximum of 8 Region descriptors are supported. That is, you can 
 *   configure 8 Region descriptors.
 *************************************************************************
 * Master0: C0-core
 * Master1: 
 * Master2: eDMA
 * Master3: 
 * -------------------------------------------------------------------
 * Slave0: Flash(0x00000000~0x1FFFFFFF),   if error->MPU_EAR0/MPU_EDR0
 * Slave2: SRAM(0x40000000~0x5FFFFFFF),    if error->MPU_EAR1/MPU_EDR1
 * Slave7: PBRIDGE(0xC0000000~0xFFFFFFFF), if error->MPU_EAR2/MPU_EDR2
 ************************************************************************/



/* @brief Switch to user mode.
 * @note  For non-core bus masters, the access attribute information is 
 *          typically wired to Supervisor, refer EDRn_EATTR_t. */
void Enter_UserMode(void)
{
    vuint32_t data = 0;

    MFMSR(data); /* PRQA S 1006 */
    data |= (MSR_PR);
    MTMSR(data); /* PRQA S 1006 */
}

/* @brief Write Start/End Address(Word 0/Word 1).
 * @note  ENDADDR >= SRTADDR.
 * @param rgdNum: refer: MPU_RGDn_t(0~7).
 *        startAddr: 32bits(Significant bits: 27 bits high)
 *        endAddr: 32bits(Significant bits: 27 bits high)
 * @ret_val 0:success, 1:fail
 * */
uint32_t MPU_RGDnWord0Word1_Set(uint8_t rgdNum, uint32_t startAddr, uint32_t endAddr)
{
    /* Software check */
    if (startAddr > endAddr)
    {
        return 1u;
    }

    MPU.RGD[rgdNum].WORD0.R = (startAddr);
    MPU.RGD[rgdNum].WORD1.R = (endAddr);

    return 0u;
}
